// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;

@test fn oneblock() void = {
	let key: [_]u8 = [
		0xfe, 0x7f, 0xec, 0x77, 0x1b, 0xa4, 0x28, 0xc7, 0xf8, 0x82,
		0x62, 0x2b, 0xe7, 0x60, 0x59, 0xf5, 0x25, 0x71, 0xda, 0x8b,
		0x8f, 0x72, 0x04, 0xe4, 0x38, 0xa2, 0xa5, 0x98, 0x45, 0x14,
		0xff, 0xbe, 0xce, 0xf6, 0xaa, 0xcc, 0xe1, 0x86, 0xc2, 0x2b,
		0x9e, 0x55, 0x86, 0x23, 0x71, 0x96, 0xa3, 0xb1, 0xa1, 0xf9,
		0x11, 0x4d, 0x86, 0xf8, 0x7e, 0xd1, 0xae, 0x5a, 0x3d, 0x4a,
		0x99, 0x65, 0xd0, 0xb1,
	];
	let sector1: [_]u8 = [
		0xe4, 0x60, 0xb0, 0x44, 0x7c, 0xc0, 0xbe, 0x63, 0xbb, 0x88,
		0x62, 0xab, 0x6e, 0x49, 0xde, 0xea,
	];
	let crypto1: [_]u8 = [
		0x6f, 0x3c, 0x46, 0xd8, 0x36, 0x25, 0x7d, 0x70, 0x00, 0xc3,
		0x94, 0x92, 0x48, 0x18, 0xa1, 0x67,
	];

	let result: [16]u8 = [0...];

	let b = xts();
	init(&b, key);

	encrypt(&b, result, sector1, 1);
	assert(bytes::equal(crypto1, result));

	decrypt(&b, result, crypto1, 1);
	assert(bytes::equal(sector1, result));
};

@test fn multiblock() void = {
	const key: [_]u8 = [
		0x61, 0x7f, 0x3d, 0x06, 0xa2, 0x18, 0x4b, 0x63, 0xd7, 0xad,
		0xfa, 0x07, 0xe7, 0x57, 0x6c, 0x2a, 0xb7, 0xb4, 0xce, 0x55,
		0x3d, 0x90, 0xd0, 0x87, 0xdf, 0xc6, 0xf4, 0x72, 0x4c, 0x54,
		0xcd, 0xda, 0x93, 0x0b, 0xb5, 0x40, 0x4b, 0xab, 0x10, 0x85,
		0x6b, 0xf7, 0x45, 0xc7, 0xda, 0x5e, 0x6e, 0x69, 0xdf, 0x68,
		0xdd, 0xb8, 0xdd, 0xd9, 0xc2, 0xe6, 0x85, 0x2e, 0x25, 0x7a,
		0xb4, 0xfc, 0x97, 0x20,
	];
	const sector11: [_]u8 = [
		0xc9, 0x41, 0xae, 0x7d, 0x5c, 0x0b, 0xa5, 0xb1, 0x31, 0xee,
		0x2e, 0x4b, 0xf8, 0x74, 0x29, 0xc5, 0x39, 0x9b, 0xfe, 0x2e,
		0x51, 0xb8, 0xab, 0xaa, 0x72, 0x49, 0x04, 0x43, 0x2a, 0x4d,
		0x50, 0xdc,
	];
	const crypto11: [_]u8 = [
		0x30, 0xa3, 0x8f, 0x05, 0x49, 0x1e, 0x4d, 0xda, 0x47, 0x44,
		0xae, 0xff, 0x7f, 0x4d, 0x4e, 0xb5, 0xca, 0xb2, 0xfc, 0xa9,
		0x70, 0x55, 0x1a, 0x05, 0xa1, 0x2a, 0xd8, 0x34, 0x6f, 0x9b,
		0x02, 0xc0,
	];
	const sector14: [_]u8 = [
		0x2e, 0x95, 0x4d, 0xa7, 0x1f, 0xfb, 0xd3, 0xc8, 0x8e, 0x6f,
		0x07, 0xab, 0x75, 0x53, 0xfc, 0x87, 0x05, 0xe8, 0xe8, 0x5a,
		0x47, 0xbc, 0xc9, 0x12, 0x0c, 0xc4, 0x09, 0x30, 0x86, 0x91,
		0xcc, 0xfa,
	];
	const crypto14: [_]u8 = [
		0xa1, 0xf8, 0xd6, 0x35, 0xcd, 0x1e, 0xbe, 0xb7, 0x64, 0xcb,
		0xbe, 0x2c, 0x58, 0xb1, 0xc0, 0xd2, 0x4e, 0x8a, 0x9e, 0x09,
		0x76, 0x08, 0xf7, 0x28, 0x94, 0xaf, 0xf8, 0x20, 0x1c, 0xbb,
		0x02, 0x9c,
	];

	let result: [32]u8 = [0...];

	let b = xts();
	init(&b, key);

	encrypt(&b, result, sector11, 11);
	assert(bytes::equal(crypto11, result));
	encrypt(&b, result, sector14, 14);
	assert(bytes::equal(crypto14, result));

	decrypt(&b, result, crypto11, 11);
	assert(bytes::equal(sector11, result));
	decrypt(&b, result, crypto14, 14);
	assert(bytes::equal(sector14, result));
};
